package chapter15;

import java.util.concurrent.Callable;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/23 14:06
 */
public class MyExecutor {
    public <V> MyFuture<V> execute(final Callable<V> task) {
        final Object lock = new Object();
        final ExecuteThread<V> thread = new ExecuteThread<>(task, lock);
        thread.start();
        MyFuture<V> future = () -> {
            synchronized (lock) {
                while (!thread.isDone()) {
                    lock.wait();
                }
                if (thread.getException() != null) {
                    throw thread.getException();
                }
                return thread.getResult();
            }
        };
        return future;
    }
}
